package com.anrongtec.zcpt.ui.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;
import android.widget.Toast;

import com.anrongtec.zcpt.R;
import com.anrongtec.zcpt.adapter.TabFocusAdapter;
import com.anrongtec.zcpt.base.BaseActivity;
import com.anrongtec.zcpt.fragment.myfocusfragment.FocusPeopleFragment;
import com.anrongtec.zcpt.fragment.myfocusfragment.NewFragment;
import com.anrongtec.zcpt.ui.activity.myfocus.FocusAdapter;
import com.anrongtec.zcpt.ui.activity.myfocus.FocusMessageActivity;
import com.zhy.view.flowlayout.TagFlowLayout;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;


/**
 * 我的关注页
 */
public class MyFocusActivity extends BaseActivity implements View.OnClickListener, TagFlowLayout.OnSelectListener {

    @BindView(R.id.iv_title_left)
    ImageView ivTitleLeft;
    @BindView(R.id.ll_back)
    LinearLayout llBack;
    @BindView(R.id.tv_title_help)
    TextView tvTitleHelp;
    @BindView(R.id.ll_title_help)
    LinearLayout llTitleHelp;
    @BindView(R.id.tv_stitle)
    TextView tvStitle;
    @BindView(R.id.ll_title_setting)
    LinearLayout llTitleSetting;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tl_focus_layout)
    TabLayout tlFocusLayout;
    @BindView(R.id.vp_focus_pager)
    ViewPager vpFocusPager;
    @BindView(R.id.activity_my_focus)
    LinearLayout activityMyFocus;
    @BindView(R.id.ll_focus_add)
    LinearLayout llFocusAdd;
    private BackgroundDarkPopupWindow paymentWindow;
    private View mMenuView;
    private ArrayList<String> tagTitleData1;
    private ArrayList<String> tagTitleData2;
    private ArrayList<String> tagTitleData3;
    private ArrayList<String> tagTitleData4;
    private ArrayList<String> tagTitleData5;
    private EditText et_FocusAdd;//popupwindow 添加关注词
    private TagFlowLayout id_Flow_layout;//标签流
    private ImageView iv_focus_add;
    private List<Fragment> mFragment = new ArrayList<Fragment>();//fragment


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTitleShow(false);
        setContentView(R.layout.activity_my_focus);
        ButterKnife.bind(this);
        initTopBar(this, true, "我的关注", "设置关注");
        initView();
        initDataBean();
    }

    private void initView() {
        llTitleSetting = (LinearLayout) findViewById(R.id.ll_title_setting);
    }

    private void initDataBean() {
        tagTitleData1 = new ArrayList<>();
        tagTitleData2 = new ArrayList<>();
        tagTitleData3 = new ArrayList<>();
        tagTitleData4 = new ArrayList<>();
        tagTitleData5 = new ArrayList<>();
        ArrayList<String> list = new ArrayList<>();
        {
            list.add("新闻 ");
            list.add("核查核录 ");
            list.add("流浪人 ");
            list.add("重点人 ");
            list.add("重点车辆 ");
        }
        mFragment.add(new NewFragment());
        mFragment.add(new FocusPeopleFragment());
        mFragment.add(new FocusPeopleFragment());
        mFragment.add(new FocusPeopleFragment());
        mFragment.add(new FocusPeopleFragment());


        TabFocusAdapter adapter = new TabFocusAdapter(getSupportFragmentManager(), mFragment, list);
        vpFocusPager.setAdapter(adapter);
        tlFocusLayout.setupWithViewPager(vpFocusPager);
    }


    @OnClick({R.id.ll_focus_add,R.id.ll_title_setting})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ll_focus_add:
                startActivity(new Intent(this, FocusMessageActivity.class));
                break;
            case R.id.ll_title_setting:
                paymentWindow = new BackgroundDarkPopupWindow(this);
                // 显示窗口
                paymentWindow.showAtLocation(llTitleSetting,
                        Gravity.RIGHT | Gravity.CENTER_HORIZONTAL, 0, 0);
                // 设置layout在PopupWindow中显示的位置
                break;
        }
    }


    @Override
    public void onSelected(Set<Integer> selectPosSet) {

    }


    /**
     * popupWindow弹出的展示效果
     */
    public class BackgroundDarkPopupWindow extends PopupWindow {

        private final FocusAdapter adapter;
        private final Button btn_shape_corner;
        private Set<Integer> selectedList;

        public BackgroundDarkPopupWindow(final Activity context) {
            super(context);
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            mMenuView = inflater.inflate(R.layout.popup_focus_add, null);

            //设置SelectPicPopupWindow的View
            this.setContentView(mMenuView);
            //设置SelectPicPopupWindow弹出窗体的宽
            this.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
            //设置SelectPicPopupWindow弹出窗体的高
            this.setHeight(ViewGroup.LayoutParams.MATCH_PARENT);
            //设置SelectPicPopupWindow弹出窗体可点击
            this.setFocusable(true);
            //设置SelectPicPopupWindow弹出窗体动画效果
            this.setAnimationStyle(android.R.style.Animation_Dialog);
            //实例化一个ColorDrawable颜色为半透明
            ColorDrawable dw = new ColorDrawable(0xb0000000);
            //设置SelectPicPopupWindow弹出窗体的背景
            this.setBackgroundDrawable(dw);
            //mMenuView添加OnTouchListener监听判断获取触屏位置如果在选择框外面则销毁弹出框
            mMenuView.setOnTouchListener(new View.OnTouchListener() {

                public boolean onTouch(View v, MotionEvent event) {

                    int height = mMenuView.findViewById(R.id.pop_layout).getTop();
                    int y = (int) event.getY();
                    if (event.getAction() == MotionEvent.ACTION_UP) {
                        if (y < height) {
                            dismiss();
                        }
                    }
                    return true;
                }
            });
            et_FocusAdd = (EditText) mMenuView.findViewById(R.id.et_focus_add);
            iv_focus_add = (ImageView) mMenuView.findViewById(R.id.iv_focus_add);
            id_Flow_layout = (TagFlowLayout) mMenuView.findViewById(R.id.id_flowlayout);
            btn_shape_corner = (Button) mMenuView.findViewById(R.id.btn_shape_corner);
            adapter = new FocusAdapter(tagTitleData1, context);
            btn_shape_corner.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (tagTitleData1.size() > 0) {
                        for (int i : selectedList) {
                            tagTitleData1.remove(i);
                        }
                        adapter.notifyDataChanged();
                    } else {
                        Toast.makeText(context, "请先选中标签", Toast.LENGTH_SHORT).show();
                    }
                }
            });
            id_Flow_layout.setAdapter(adapter);
            iv_focus_add.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (et_FocusAdd.getText().length() > 0) {
                        String etFocus = et_FocusAdd.getText().toString();
                        tagTitleData1.add(etFocus);
                        et_FocusAdd.setText("");
                        adapter.notifyDataChanged();
                    } else {
                        Toast.makeText(context, "请先添加关键字", Toast.LENGTH_SHORT).show();
                    }
                }
            });
            id_Flow_layout.setOnSelectListener(new TagFlowLayout.OnSelectListener() {
                @Override
                public void onSelected(Set<Integer> selectPosSet) {
                    selectedList = id_Flow_layout.getSelectedList();
                }
            });

        }
    }
}
